//
//  OilPaymentViewController.m
//  阿帕
//
//  Created by mac on 2020/5/26.
//  Copyright © 2020 GCY. All rights reserved.
//

#import "OilPaymentViewController.h"
#import "OilCardCell.h"

@interface OilPaymentViewController () <UITableViewDataSource, UITableViewDelegate>
//@property (weak, nonatomic) IBOutlet UIView *mainBgView;
//@property (weak, nonatomic) IBOutlet UIView *topBgView;
//@property (weak, nonatomic) IBOutlet UILabel *topTipTextLabel;
//@property (weak, nonatomic) IBOutlet UILabel *oilCardLabel;
//@property (weak, nonatomic) IBOutlet UILabel *bottomTipTextLabel;
//@property (weak, nonatomic) IBOutlet UIImageView *qrImageView;
@property (weak, nonatomic) IBOutlet UITableView *container;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation OilPaymentViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.navigationItem.title = @"加油付款";

    self.view.backgroundColor = [UIColor cy_colorWithHexString:@"#1867E0"];
    
//    cut(_mainBgView, 10);
    
//    _topTipTextLabel.textColor = [UIColor cy_colorWithHexString:@"#0C8CED"];
//    _bottomTipTextLabel.textColor = Dark_TextColor;
    
//    _topBgView.backgroundColor = [UIColor cy_colorWithHexString:@"#F4F8FD"];
    
//    CGFloat left = (ScreenW - 240)/2;
    
//    _qrImageView.frame = CGRectMake(0, 135, ScreenW - 210, ScreenW - 210);
//    _qrImageView.centerX = (ScreenW-30)/2;
    
    //
//    NSDictionary *userInfo = [UserDefaultUtil valueForKey:Login_Branch];
//    if ([DCSpeedy isBlankString:userInfo[@"driverQrCode"]] == NO) {
//        [_qrImageView sd_setImageWithURL:[NSURL URLWithString:userInfo[@"driverQrCode"]] placeholderImage:image(@"gcy_icon_plaholder")];
//    }
    [self setupNav];
    [self configTableView];
    [self getDriverAllOilCard];
}

- (void)setupNav {
    [DCSpeedy changeNavControllerGradientView:self andBgColor:[UIImage gradientImageWithBounds:self.navigationController.view.bounds andColors:@[[UIColor cy_colorWithHexString:@"#1867E0"], [UIColor cy_colorWithHexString:@"#1867E0"]] andGradientType:GradientDirectionLeftToRight] andTitleColor:[UIColor whiteColor]];
//    [self.navigationController.navigationBar setBackgroundColor:[UIColor cy_colorWithHexString:@"#1867E0"]];
//
    UIButton *button = [[UIButton alloc] init];
    [button setImage:[UIImage imageNamed:@"gcy_img_nav_lArrowTwo"] forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 17, 32);
    [button addTarget:self action:@selector(backButtonTapClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = backItem;
}

- (void)backButtonTapClick {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)configTableView {
    [_container setTableFooterView:[[UIView alloc] init]];
    [_container registerNib:[UINib nibWithNibName:@"OilCardCell" bundle:nil] forCellReuseIdentifier:@"OilCardCell"];
//    [_container setEstimatedRowHeight:100];
//    [_container setRowHeight:UITableViewAutomaticDimension];
    _container.backgroundColor = [UIColor cy_colorWithHexString:@"#1867E0"];
}

//获取所有司机油卡
- (void)getDriverAllOilCard {
    WEAKSELF
    
    NSDictionary *branch = [UserDefaultUtil valueForKey:Login_Branch];
    NSDictionary *params = @{@"driverCode": branch[@"code"]};
    
    [HTTPManager getNetworkRequest:CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], GASSTATIONPAYMENT_GETALLOILCARD) withParameter:params withVC:self netName:@"获取所有司机油卡" success:^(id response, NSString *msg) {
        DebugLog(@"%@", response);
        [weakSelf.dataArray addObjectsFromArray:[OilCardModel mj_objectArrayWithKeyValuesArray:response]];
        for (NSInteger i = 0; i < weakSelf.dataArray.count; i++) {
            OilCardModel *model = weakSelf.dataArray[i];
            if (i == 0) {
                [weakSelf getDriverOilQrCodeWithModel:model];
            }
        }
        [weakSelf.container reloadData];
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}

//司机机构加油二维码
- (void)getDriverOilQrCodeWithModel: (OilCardModel *)model {
    WEAKSELF;
    
    NSDictionary *branch = [UserDefaultUtil valueForKey:Login_Branch];
    NSDictionary *params = @{
        @"driverCode": branch[@"code"],
        @"branchCode":model.branchCode
    };
    
    [HTTPManager getNetworkRequest:CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], @"/gasStationPayment/getDriverOilQrCode") withParameter:params withVC:self netName:@"司机机构加油二维码" success:^(id response, NSString *msg) {
        DebugLog(@"%@", response);
        
        for (OilCardModel *modelItem in weakSelf.dataArray) {
            modelItem.isSelected = NO;
        }
        model.isSelected = YES;
        model.qrImageURLStr = response;
        [weakSelf.container reloadData];
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

#pragma TableView-DataSource-Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OilCardCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OilCardCell"];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self getDriverOilQrCodeWithModel:self.dataArray[indexPath.row]];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    OilCardModel *model = self.dataArray[indexPath.row];
    return model.isSelected ? (165 + ScreenW * 0.4) : 100;
}

@end
